package com.platform.web.provider;

import com.platform.common.base.Page;
import com.platform.web.mapper.PermissionMapper;
import com.platform.web.model.Permission;
import com.platform.web.service.IPermissionService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.*;

/**
 * 系统权限接口实现类
 * 
 * @author LiangMingHui
 *
 */
@Component("permissionService")
public class PermissionServiceImpl implements IPermissionService {

	@Resource
	private PermissionMapper permissionMapper;

	@Override
	public int deleteByPrimaryKey(String id) throws Exception {
		return permissionMapper.deleteByPrimaryKey(id);
	}

	@Override
	public int insert(Permission record) throws Exception {
		int result = permissionMapper.getPermissionCount(record);
		if (result > 0) {
			throw new Exception("存在相同的权限代码或者权限名称");
		}
		String id = UUID.randomUUID().toString();
		record.setId(id);
		return permissionMapper.insert(record);
	}

	@Override
	public int insertSelective(Permission record) throws Exception {
		int result = permissionMapper.getPermissionCount(record);
		if (result > 0) {
			throw new Exception("存在相同的权限代码");
		}
		String id = UUID.randomUUID().toString();
		record.setId(id);
		return permissionMapper.insertSelective(record);
	}

	@Override
	public int updateByPrimaryKeySelective(Permission record) throws Exception {
		int result = permissionMapper.getPermissionCount(record);
		if (result > 0) {
			throw new Exception("存在相同的权限代码或者权限名称");
		}
		return permissionMapper.updateByPrimaryKeySelective(record);

	}

	@Override
	public int updateByPrimaryKey(Permission record) throws Exception {
		int result = permissionMapper.getPermissionCount(record);
		if (result > 0) {
			throw new Exception("存在相同的权限代码或者权限名称");
		}
		return permissionMapper.updateByPrimaryKey(record);
	}

	@Override
	public Permission selectByPrimaryKey(String id) throws Exception {
		return permissionMapper.selectByPrimaryKey(id);
	}

	@Override
	public Page<Permission> selectPermissionByPage(Map<String, Object> map) throws Exception {
		Page<Permission> page = new Page<Permission>();
		page.setPageNo((Integer) map.get("pageNo"));
		page.setPageSize((Integer) map.get("pageSize"));
		map.put("page", page);
		List<Permission> results = permissionMapper.selectPermissionByPage(map);
		page.setResults(results);
		return page;
	}

	@Override
	public Map<String, Object> selectPermissionByPage(Map<String, Object> map, Page<Object> page) throws Exception {
		page.setPageNo(page.getPageNo() / page.getPageSize() + 1);
		map.put("page", page);
		List<Permission> data = permissionMapper.selectPermissionByPage(map);
		Map<String, Object> params = new HashMap<String, Object>(2);
		params.put("data", data);
		params.put("recordsTotal", page.getTotalPage());// 总页数
		params.put("recordsFiltered", page.getTotalRecord());
		return params;
	}

	@Override
	public int deleteBatchByIds(List<String> item) throws Exception {
		int result = permissionMapper.getRolePermissionCount(item);
		if (result > 0) {
			throw new Exception("删除权限项失败，存在绑定的角色权限，请先解除角色权限绑定");
		}
		int result1 = permissionMapper.deleteBatchByIds(item);
		if (result1 <= 0) {
			throw new Exception("删除权限项失败");
		}
		return result1 + result;
	}

	@Override
	public List<Permission> selectPermissionByParentId(String pid, boolean isChildrens) throws Exception {
		List<Permission> permissions = permissionMapper.selectPermissionByParentId(pid);
		if (isChildrens) {
			for (int i = 0; i < permissions.size(); i++) {
				Permission permission = permissions.get(i);
				pid = permission.getId();
				List<Permission> childrens = permissionMapper.selectPermissionByParentId(pid);
				permission.setChildrens(childrens);
			}
		}
		return permissions;
	}

	@Override
	public List<Permission> selectAllPermissionList() throws Exception {
		List<Permission> permissions = permissionMapper.selectAllPermissionList();
		List<Permission> parents = new ArrayList<Permission>();

		// 获取父节点
		for (Permission p : permissions) {
			if (StringUtils.isBlank(p.getModeulCode())) {
				parents.add(p);
			}
		}

		// 遍历获取子节点
		addChildrens(permissions, parents);
		return parents;
	}

	/**
	 * 添加子节点
	 *
	 * @param permissions
	 * @param parents
	 * @return
	 */
	private List<Permission> addChildrens(List<Permission> permissions, List<Permission> parents) {
		if (!permissions.isEmpty() && !parents.isEmpty()) {
			permissions.removeAll(parents);// 移除父节点
			List<Permission> childrens = null;
			for (Permission per : parents) {
				childrens = findChildrens(permissions, per);
				if (!childrens.isEmpty() && childrens.size() > 0) {
					per.setChildrens(childrens);
					addChildrens(permissions, childrens);
				}
			}
		}
		return parents;
	}

	/**
	 * 查找子节点
	 *
	 * @param permissions
	 * @param per
	 * @return
	 */
	private List<Permission> findChildrens(List<Permission> permissions, Permission per) {
		List<Permission> childrens = new ArrayList<Permission>();
		if (!permissions.isEmpty() && per != null) {
			for (Permission p : permissions) {
				if (per.getPermissionCode().equals(p.getModeulCode())) {
					childrens.add(p);
				}
			}
		}
		return childrens;
	}
}
